package com.ccc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.dto.Result;
import com.ccc.dto.UserDTO;
import com.ccc.entity.Follow;
import com.ccc.entity.User;
import com.ccc.mapper.FollowMapper;
import com.ccc.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccc.service.IUserService;
import com.ccc.utils.RedisConstants;
import com.ccc.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    @Override
    public Result follow(Long followId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        if(userId == null){
            return Result.fail("请先登录");
        }
        Follow follow = new Follow();
        if(isFollow){
            //如果关注, 就加入set集合
            follow.setUserId(userId);
            follow.setFollowUserId(followId);
            boolean isSaved = save(follow);
            if(isSaved){
                stringRedisTemplate.opsForSet()
                        .add(RedisConstants.FOLLOW_KEY+userId,followId.toString());
            }
        }
        else{
            boolean isRemoved =  remove(new QueryWrapper<Follow>()
                    .eq("follow_user_id",followId).eq("user_id",userId));
            if(isRemoved){
                stringRedisTemplate.opsForSet()
                        .remove(RedisConstants.FOLLOW_KEY+userId,followId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followId) {
        //关注返回true,未关注返回false
        Long userId = UserHolder.getUser().getId();
        Integer count = query().eq("follow_user_id",followId).eq("user_id",userId).count();
        return Result.ok(count>0);
    }

    @Override
    public Result followCommons(Long id) {
        Long userId = UserHolder.getUser().getId();
        Set<String> intersect = stringRedisTemplate.opsForSet()
                .intersect(RedisConstants.FOLLOW_KEY + userId, RedisConstants.FOLLOW_KEY + id);
        if(intersect == null || intersect.isEmpty()){
            return Result.fail("没有>>");
        }
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);

    }
}
